package gggameserver.games;

import gggameserver.exceptions.GGIllegalMoveFormat;
import gggameserver.exceptions.GGTimeout;
import gggameserver.games.connection.GGConnection;
import gggameserver.games.connection.GGSocketConnection;
import gggameserver.games.connection.ProcessConnection;

public abstract class Player {

	private Board board;
	private int number = -1;
	private GGConnection connection;
	private boolean handshakeSent = false;

	public Player(Board board) {
		this.board = board;
	}

	public final Board getBoard() {
		return this.board;
	}

	protected void setPlayerNumber(int nr) {
		if (this.number < 0)
			this.number = nr; // TODO
	}

	public void setConnection(GGConnection con) {
		this.connection = con;
	}

	public void sendCommand(String cmd) {
		connection.sendCommand(cmd);
	}

	public String receiveCommand(long timeout) throws GGTimeout {
		return connection.receiveCommand(timeout);
	}

	// starting from 1
	public int getPlayerNumber() {
		return this.number;
	}

	public Move getMove(long timeout) throws GGTimeout, GGIllegalMoveFormat {
		return this.board.commandToMove(this.receiveCommand(timeout));
	}

	public void sendMove(Move move, boolean nextPlayer) {
		this.sendCommand(this.board.moveToCommand(move));
	}

	public boolean isHandshakeSent() {
		return this.handshakeSent;
	}

	public void setHandshakeSent() {
		this.handshakeSent = true;
	}

	public void sendHandshake() {
	}

	public void sendStart(boolean starts) {
		// TODO
		if (getPlayerNumber() == 1)
			this.sendCommand("start");
	}

	public void sendResult(Result result) {
		if (result.getPlace() == 1)
			this.sendCommand("win");
		else
			this.sendCommand("loss");
	}

	public String getConnectionType() {
		if (connection instanceof GGSocketConnection) {
			return "TCP";
		} else if (connection instanceof ProcessConnection) {
			return "Command";
		} else {
			return "Human";
		}
	}

    public String getName() {
        return "";
    }

}
